<?php
declare(strict_types=1);

namespace App\Controller\Admin\V1\Material;

use App\Controller\Admin\AdminController;
use App\Model\MaterialSheetAssign;
use App\Repositories\MaterialAssignRepository;
use App\Repositories\MaterialFlowRepository;
use App\Repositories\MaterialProjectRepository;
use App\Repositories\MaterialProjectSheetRepository;
use App\Repositories\MaterialSheetAssignRepository;
use App\Request\StoreMaterialSheetRequest;
use Hyperf\DbConnection\Db;

class SheetController extends AdminController
{
    public function list(){
        $name = $this->request->input('title',null);
        $rows = $this->request->input('rows',10);
        $project_id = $this->request->input('project_id',0);
        $search=[];
        if($name){
            $search['name']=[
                'field' => 'sheet_name',
                'value' => $name,
                'method' => 'like'
            ];
        }
        $search['project_id']=[
            'field' => 'project_id',
            'value' => $project_id
        ];
        $orderBy= [
            [
                'field' => 'listorder',
                'sort' => 'asc'
            ]
        ];
        $datas = MaterialProjectSheetRepository::getInstance()->getList('*',$search,['limit'=>$rows],$orderBy);
        return $this->response->success($datas);
    }

    public function detail(){
        $id = $this->request->input('id');
        $sheet = MaterialProjectSheetRepository::getInstance()->getById($id);
        return $this->response->success($sheet);
    }

    public function delete(){
        $id = $this->request->input('id');
        $sheet = MaterialProjectSheetRepository::getInstance()->getById($id);
        $count = MaterialAssignRepository::getInstance()->count('id',[[
            'field' => 'material_id',
            'value' => $sheet->material_id
        ]]);
        if($count){
            return $this->response->error(10020,'清单已分配单位');
        }
        DB::beginTransaction();
        try{
            $result = MaterialProjectSheetRepository::getInstance()->delete($id);
            if(!$result){
                throw new \Exception('删除失败');
            }
            $count_assign = MaterialSheetAssignRepository::getInstance()->count('id',[[
                'field' => 'sheet_id',
                'value' => $id
            ]]);
            if($count_assign){
                $assign_ids = MaterialSheetAssign::query()->where('sheet_id',$id)
                    ->select('id')->get()->pluck('id')->toArray();
                $count_flow = MaterialFlowRepository::getInstance()->count('id',[[
                    'field' => 'material_assign_id',
                    'value' => $assign_ids,
                    'method' => 'in'
                ]]);
                if($count_flow){
                    $res_flow = MaterialFlowRepository::getInstance()->deleteAll([[
                        'field' => 'material_assign_id',
                        'value' => $assign_ids,
                        'method' => 'in'
                    ]]);
                    if(!$res_flow){
                        throw new \Exception('流程信息删除失败');
                    }
                }
                $res_assign = MaterialSheetAssignRepository::getInstance()->deleteAll([[
                    'field' => 'sheet_id',
                    'value' => $id
                ]]);
                if(!$res_assign){
                    throw new \Exception('分配信息删除失败');
                }
            }
            DB::commit();
        }catch(\Throwable $e){
            DB::rollBack();
            return $this->response->error(10012,$e->getMessage());
        }
//        $result = MaterialProjectSheetRepository::getInstance()->delete($id);
//        if($result){
//            return $this->response->success($result['data']);
//        }
        return $this->response->success([]);
    }

    public function store(StoreMaterialSheetRequest $request){
        $admin = $this->request->getAttribute('admin');
        $params = $request->all();
        $params['admin_id'] = $admin->id;
        $project = MaterialProjectRepository::getInstance()->getById($params['project_id']);
        if(!$project){
            return $this->response->error(10012,'项目不存在');
        }
        $params['material_id'] = $project->material_id;
        $count = MaterialAssignRepository::getInstance()->count('id',[[
            'field' => 'material_id',
            'value' => $project->material_id
        ]]);
        if($count&&!$params['id']){
            return $this->response->error(10020,'清单已分配单位');
        }
        DB::beginTransaction();
        try{
            $result = MaterialProjectSheetRepository::getInstance()->store($params);
            if(!$result['success']){
                throw new \Exception('项目保存失败');
            }
            $project = $result['data'];
            DB::commit();
        }catch(\Throwable $e){
            DB::rollBack();
            return $this->response->error(10012,$e->getMessage());
        }
        return $this->response->success([
            'id' => $project->id
        ]);
    }

}